<?php
namespace person_info {
    class person
    {
        const p_EnCode = "EnCode";
        const p_Account = "Account";
        const p_Password = "Password";
        const p_RealName = "RealName";
        const p_Gender = "Gender";
        const p_Birthday = "Birthday";
        const p_Mobile = "Mobile";
        const p_Telephone = "Telephone";
        const p_Email = "Email";
        const p_OrganizeId = "OrganizeId";
        const p_DepartmentId = "DepartmentId";
        const p_RoleId = "RoleId";
        const p_DutyId = "DutyId";
        const p_PreviousVisit = "PreviousVisit";
        const p_LastVisit = "LastVisit";
        const p_LogOnCount = "LogOnCount";
        const p_DeleteMark = "DeleteMark";
        const p_EnableMark = "EnableMark";
        const p_CreateDate = "CreatDate";
        const p_CreateUserId = "CreatUserId";
        const p_ModifyDate = "ModifyDate";
        const p_ModifyUserId = "ModifyUserId";

        public $person_array = array();
        public $person_role = array();
        public $person_item = array();

        function __construct(array $par, $db)
        {
            $this->make_person_array($par);
            $key = $this->person_array[self::p_Account];
            $from = $db->query("SELECT UserId from Base_user where Account = '$key'");
            $row = $from->fetch();
            $this->person_array['UserId'] = $row['UserId'];

            $key = $this->person_array['UserId'];
            $i = 0;
            $from = $db->query("SELECT ObjectId from Base_UserRelation where UserId = '$key'");
            while ($row = $from->fetch()) {
                $this->person_role[$i] = $row['ObjectId'];
                $i++;
            }
            $i = 0;
            foreach ($this->person_role as $key => $value) {
                $from = $db->query("SELECT FullName from Base_Role where RoleId = '$value' AND Category = '1'");
                $row = $from->fetch();
                if($row == null){
                    unset($this->person_role[$key]);
                }else{
                    $this->person_array['RoleName'][$i] = $row['FullName'];
                    $i++;
                }
            }
            reset($this->person_role);
            $i = 0;
            foreach ($this->person_role as $value) {
                $from = $db->query("SELECT ItemId from Base_Authorize where ObjectId = '$value' AND ItemType = '1'");
                while ($row = $from->fetch()) {
                    $this->person_item[$i] = $row['ItemId'];
                    $i++;
                }
            }
            $i = 0;
            foreach ($this->person_item as $value) {
                $from = $db->query("SELECT FullName from Base_Module where ModuleId = '$value'");
                $row = $from->fetch();
                $this->person_array['ModuleName'][$i] = $row['FullName'];
                $i++;
            }
        }

        private function make_person_array(array $par)
        {
            if (count($par) != 22) {
                return false;
            } else {
                $this->person_array[self::p_EnCode] = "";
                $this->person_array[self::p_Account] = "";
                $this->person_array[self::p_Password] = "";
                $this->person_array[self::p_RealName] = "";
                $this->person_array[self::p_Gender] = "";
                $this->person_array[self::p_Birthday] = "";
                $this->person_array[self::p_Mobile] = "";
                $this->person_array[self::p_Telephone] = "";
                $this->person_array[self::p_Email] = "";
                $this->person_array[self::p_OrganizeId] = "";
                $this->person_array[self::p_DepartmentId] = "";
                $this->person_array[self::p_RoleId] = "";
                $this->person_array[self::p_DutyId] = "";
                $this->person_array[self::p_PreviousVisit] = "";
                $this->person_array[self::p_LastVisit] = "";
                $this->person_array[self::p_LogOnCount] = "";
                $this->person_array[self::p_DeleteMark] = "";
                $this->person_array[self::p_EnableMark] = "";
                $this->person_array[self::p_CreateDate] = "";
                $this->person_array[self::p_CreateUserId] = "";
                $this->person_array[self::p_ModifyDate] = "";
                $this->person_array[self::p_ModifyUserId] = "";
                $i = 0;
                foreach ($this->person_array as $key => $value) {
                    if (is_string($par[$i])) {
                        $this->person_array[$key] = $par[$i];
                        $i++;
                    } else {
                        return false;
                    }
                }
            }
        }
    }
}


